1、导入相关的pom依赖(struts、自定义标签库的依赖)
<!-- 5.3、jstl、standard -->
<dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>taglibs</groupId> <artifactId>standard</artifactId> <version>1.1.2</version> </dependency><!-- 5.4、tomcat-jsp-api -->
<dependency> <groupId>org.apache.tomcat</groupId> <artifactId>tomcat-jsp-api</artifactId> <version>8.0.47</version> </dependency> 2、分页的tag类导入、z.tld、完成web.xml的配置pageTag.java
package com.huang.crud.tag;import java.io.IOException;import java.util.Map;import java.util.Map.Entry;import javax.servlet.jsp.JspException;import javax.servlet.jsp.JspWriter;import javax.servlet.jsp.tagext.BodyTagSupport;import com.huang.crud.util.PageBean;public class PageTag extends BodyTagSupport { private static final long serialVersionUID = -8476740758233598045L; private PageBean pageBean; @Override public int doStartTag() throws JspException { JspWriter out = pageContext.getOut(); try { out.write(toHTML()); } catch (IOException e) { e.printStackTrace(); } return super.doStartTag(); } public String toHTML() {// pageBean=new PageBean(); StringBuffer sb=new StringBuffer(); //下一次请求提交到后台的表单html代码拼接 sb.append(" "); sb.append(""); sb.append(""); sb.append("
z.tid
dj 1.1 core library dj core 1.1 d /huang page com.huang.crud.tag.PageTag JSP pageBean true true
web.xml
Archetype Created Web Application encodingFiter com.huang.crud.util.EncodingFiter encodingFiter /* struts2 org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter struts2 *.action
3、dao层去访问数据
ClazzDao.java
package com.huang.crud.dao;import java.sql.SQLException;import java.util.List;import com.huang.crud.entity.Clazz;import com.huang.crud.util.BaseDao;import com.huang.crud.util.PageBean;import com.huang.crud.util.StringUtils;public class ClazzDao extends BaseDao{ public List list(Clazz clazz,PageBean pageBean) throws InstantiationException, IllegalAccessException, SQLException{ String sql= "select * from t_struts_class where true "; String cname= clazz.getCname(); int cid=clazz.getCid(); if(cid!=0) { sql +=" and cid ="+cid; } if(StringUtils.isNotBlank(cname)) { sql +=" and cname like '%"+cname+"%'"; } return super.executeQuery(sql,Clazz.class , pageBean); } public int add(Clazz clazz) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, SQLException, InstantiationException { String sql= "insert into t_struts_class values(?,?,?,?)"; return super.executeUpdate(sql,new String[] {"cid","cname","cteacher","pic"}, clazz); } public int del(Clazz clazz) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, SQLException, InstantiationException { String sql= "delete from t_struts_class where cid=?"; return super.executeUpdate(sql,new String[] {"cid"}, clazz); } public int edit(Clazz clazz) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, SQLException, InstantiationException { String sql= "update t_struts_class set cname=?,cteacher=?,pic=? where cid=?"; return super.executeUpdate(sql,new String[] {"cname","cteacher","pic","cid"}, clazz); }}
BaseDao.java
package com.huang.crud.util;import java.lang.reflect.Field;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.util.ArrayList;import java.util.List;import com.huang.crud.entity.Clazz;public class BaseDao{ /** * T=Student.class * * @param sql 决定查询哪张表的数据 * @param clz 查询出来的数据封装到哪个实体类中 * @param pageBean 决定是否分页 * @return * @throws * @throws InstantiationException * @throws SQLException * @throws IllegalAccessException * @throws IllegalArgumentException * @throws InstantiationException */ public List executeQuery(String sql,Class clz,PageBean pageBean) throws SQLException, IllegalArgumentException, IllegalAccessException, InstantiationException { List ls=new ArrayList<>(); Connection con=DBAccess.getConnection(); PreparedStatement ps = null; ResultSet rs = null; if(pageBean!=null && pageBean.isPagination()) { //该分页了 String countSql=getCountSql(sql);//获取符合记录的行数的Sql语句 try { ps=con.prepareStatement(countSql); } catch (SQLException e) { e.printStackTrace(); } try { rs=ps.executeQuery(); } catch (SQLException e) { e.printStackTrace(); } if(rs.next()) { pageBean.setTotal(rs.getLong(1)+"");//给pageBean的总页数赋值 } String pageSql=getPageSql(sql,pageBean);//拼接上符合条件的分页查询sql语句 ps=con.prepareStatement(pageSql); rs=ps.executeQuery(); } else { ps = con.prepareStatement(sql); rs = ps.executeQuery(); } try { while (rs.next()) { // ls.add(new Book(rs.getInt("bid"), // rs.getString("bname"), // rs.getFloat("price"))); /** * 1、创建了一个book对象 * 2、从ResultSet结果集中获取值放入Book对象属性中 * 2.1获取到book的属性对象 * 2.2给属性对象赋值 * 3、将已经有值的Book对象放进list集合中 * */ T t = (T) clz.newInstance(); Field[] fields = clz.getDeclaredFields(); for (Field field : fields) { field.setAccessible(true); field.set(t, rs.getObject(field.getName())); } ls.add(t); } } finally { DBAccess.close(con,ps,rs); } return ls; } /** * 将原生sql拼接出符合条件的某一页的数据查询 * @param sql * @return */ private String getPageSql(String sql,PageBean pageBean) { return sql + " limit "+pageBean.getStartIndex()+","+pageBean.getRows(); } /** * 用原生sql拼接出查询符合条件的记录数 * @param sql * @return */ private String getCountSql(String sql) { return "select count(1) from ("+sql+") t"; } /** * 通用的增删改方法 * @param sql 增删改的sql语句 * @param attrs ?所代表的实体类的属性 * @param t 实体类的实例 * @return * @throws SQLException * @throws NoSuchFieldException * @throws SecurityException * @throws IllegalArgumentException * @throws IllegalAccessException * @throws InstantiationException */ public int executeUpdate(String sql, String[] attrs, T t) throws SQLException, NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, InstantiationException { Connection con = DBAccess.getConnection(); PreparedStatement pst = con.prepareStatement(sql); for (int i = 0; i < attrs.length; i++) { Field field = t.getClass().getDeclaredField(attrs[i]); field.setAccessible(true); pst.setObject(i+1, field.get(t)); } return pst.executeUpdate(); }}
4、在struts_sy.xml进行配置
struts_sy.xml
/clzList.jsp /clzEdit.jsp /clz_list
5、写jsp
clzList.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib uri="/huang" prefix="d" %>增加主界面 小说目录
编号 | 班级名称 | 班级图片 | 操作 | |
${b.cid } | ${b.cname } | ${b.cteacher } | ${b.pic } | 修改 删除 文件上传 |
clzEdit.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>Insert title here
运行结果: